package net.bezrabotnih.server.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;




@RooJavaBean
@RooToString
@RooEntity(identifierType = String.class,identifierField="id")
public class UserAccount {
	
	public static int PERFORMER=0;
	
	public static int CUSTOMER=1;
	
    public static int MALE=0;
	
	public static int FEMALE=1;
	
	@NotNull
	private String email;
	
	private String password;
	
	private Integer accountType;
	@NotNull
	private String name;
	
	private String surname;
	
	
	private List<String> jobTypeIds;
	
	private Integer age;
	
	private Integer sex;
	
	private String phoneNumber;
	
	private Thumbnail thumbnail;
	
	private String thumbnailUrl;
	
	
	
	
	public String getThumbnailUrl() {
		return thumbnailUrl;
	}
	public void setThumbnailUrl(String thumbnailUrl) {
		this.thumbnailUrl = thumbnailUrl;
	}
	
	
	@Transactional
	public static UserAccount currentUserAccount(){
	      UserService userService=UserServiceFactory.getUserService();
	      User user=userService.getCurrentUser();
	      UserAccount userAccount=null;
	      if (user!=null){
	    	  String email=user.getEmail();
	    	  userAccount=UserAccount.findUserAccountByEmail(email);
	    	  if (userAccount==null){
	    		  userAccount=new UserAccount();
	    		  userAccount.setEmail(email);
	    		  userAccount.setName(user.getNickname());
	    		  userAccount.setAccountType(UserAccount.PERFORMER);
	    		  userAccount.setSex(UserAccount.MALE);
	    		  userAccount.persist();
	    	  }
	    	  
	    	  LoginAction loginAction=new LoginAction();
	    	  loginAction.setUserAccountId(userAccount.getId());
	    	  loginAction.setDate(new Date());
	    	  loginAction.persist();
	    	  
	    	  userAccount=UserAccount.findUserAccount(userAccount.getId());
	      }
	      return userAccount;
	}    
	@Transactional
    public static UserAccount findUserAccountByEmail(String email) {
        if (email == null || email.length() == 0) return null;
        try {
            return (UserAccount) entityManager().createQuery("SELECT o FROM UserAccount o WHERE o.email = :email").setParameter("email", email).getSingleResult();
        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
            return null;
        } catch (javax.persistence.NoResultException e) {
            return null;
        }
    }
	
	@Transactional
	public static List<UserAccount> findUserAccounts(List<String> ids){
		
		 if (ids == null || ids.size() == 0) return null;
	        try {
	            return (List<UserAccount>) entityManager().createQuery("SELECT o FROM UserAccount o WHERE o.id MEMBER OF :ids").setParameter("ids", ids).getResultList();
	        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
	            return null;
	        } catch (javax.persistence.NoResultException e) {
	            return null;
	        }
		
	}
	
	@Transactional
    public static List<UserAccount> findApplicantsByJobId(String jobId) {
        if (jobId == null || jobId.length() == 0) return null;
        try {
        	Job job=Job.findJob(jobId);
            if (job==null||job.getApplicantIds()==null||job.getApplicantIds().size()==0) return null;
            return (List<UserAccount>) entityManager().createQuery("SELECT o FROM UserAccount o WHERE o.id MEMBER OF :ids").setParameter("ids", job.getApplicantIds()).getResultList();
        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
            return null;
        } catch (javax.persistence.NoResultException e) {
            return null;
        }
    }
	
	
}
